//
//  MyOpinionListController.m
//  Stock
//
//  Created by sun black on 11-10-16.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyOpinionListController.h"
#import "StockListController.h"
#import "MyStock.h"

@implementation MyOpinionListController
@synthesize tableView=_tableView;
@synthesize service;
@synthesize centerTextLabel;
@synthesize rightTextLabel;



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.service = [[[StockAppService alloc] initWithDelegate:self] autorelease];
    [super viewDidLoad];
    if ([key isEqualToString:@"weekgrowth"]) {
        centerTextLabel.text = @"热度排名";
        rightTextLabel.text = @"排名变化";
    }
    
}


-(void)queryStocks{
    [super queryStocks];
    [service queryNewOpinionWithIndex:currentPage andType:key];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    [self.service cancleAllRequest];
    self.service = nil;
    self.key = nil;
    self.centerTextLabel=nil;
    self.rightTextLabel = nil;
}
-(void)dealloc{
    self.tableView = nil;
    [self.service cancleAllRequest];
    self.service = nil;
    self.key = nil;
    self.centerTextLabel=nil;
    self.rightTextLabel = nil;
    [super dealloc];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [quotationStocks count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        UIColor *textColor = [UIColor whiteColor];
        UIColor *bgColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.backgroundColor = bgColor;
        cell.textLabel.textColor = textColor;
        cell.textLabel.font = [UIFont systemFontOfSize:17.0];
        UILabel *centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 11.0, 85.0, 20.0)];
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 11.0, 80.0, 20.0)];
        centerLabel.textAlignment = UITextAlignmentRight;
        rightLabel.textAlignment = UITextAlignmentRight;
        centerLabel.textColor = textColor;
        centerLabel.backgroundColor = bgColor;
        rightLabel.textColor = textColor;
        rightLabel.backgroundColor = bgColor;
        centerLabel.tag = 198;
        rightLabel.tag = 199;
        [cell.contentView addSubview:centerLabel];
        [cell.contentView addSubview:rightLabel];
        [centerLabel release];
        [rightLabel release];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(280.0, 11.0, 8.0, 20.0)];
        imageView.image = [UIImage imageNamed:@"icon_up.png"];
        imageView.tag = 200;
        [cell.contentView addSubview:imageView];
        [imageView release];
        
    }    
    
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg2.png";
    if (index%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    NSDictionary *dict = [quotationStocks objectAtIndex:indexPath.row];
    UILabel *centerLabel = (UILabel *)[cell viewWithTag:198];
    UILabel *rightLabel = (UILabel *)[cell viewWithTag:199];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:200];
    imageView.hidden = YES;
    
    NSString *centerText=nil;
    NSString *rightText=nil;
    
    if([key isEqualToString:@"month"]) {
        centerText = [NSString stringWithFormat:@"%0.1f-%0.1f",[[dict objectForKey:@"monthLowPrice"] floatValue],[[dict objectForKey:@"monthHighPrice"] floatValue]];
        rightText = [dict objectForKey:@"monthPriceRange"];
    }else if([key isEqualToString:@"week"]){
        centerText = [NSString stringWithFormat:@"%0.1f-%0.1f",[[dict objectForKey:@"weekLowPrice"] floatValue],[[dict objectForKey:@"weekHighPrice"] floatValue]];
        rightText = [dict objectForKey:@"weekPriceRange"];
    }else if([key isEqualToString:@"weekgrowth"]){
        int weekRankGrowth = [[dict objectForKey:@"weekRankGrowth"] intValue];
        if (weekRankGrowth<0) {
            imageView.image = [UIImage imageNamed:@"icon_down_green.png"];
        }else if(weekRankGrowth>0){
            imageView.image = [UIImage imageNamed:@"icon_up.png"];
        }else{
            imageView.image = nil;
        }
        imageView.hidden = NO;
        centerText = [NSString stringWithFormat:@"%d",[[dict objectForKey:@"weekRank"] intValue]];
        rightText = [NSString stringWithFormat:@"%d",abs(weekRankGrowth)];
        
    }
    
    cell.textLabel.text = [dict objectForKey:@"stockName"];
    centerLabel.text = centerText;
    rightLabel.text = rightText;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = [quotationStocks objectAtIndex:indexPath.row];
    
    MyStock *myStock = [[MyStock alloc] init];
    myStock.name = [dict objectForKey:@"stockName"];
    myStock.uuid = [dict objectForKey:@"stockId"];
    
    [StockContext shareInstance].remoteMyStocks = [NSMutableArray arrayWithObject:myStock];
    
    StockListController *stockListController = [[StockListController alloc] initWithNibName:@"StockListController" bundle:nil];
    stockListController.myStock = myStock;
    StockAppDelegate *delegate = (StockAppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.navigationController pushViewController:stockListController animated:YES];
    [stockListController release];
    
    [myStock release];
}



@end
